package xyz.bali16.module.sign.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.application.entity.Role;
import xyz.bali16.module.sign.entity.SignPoint;
import xyz.bali16.module.sign.model.SignPointAddParam;
import xyz.bali16.module.sign.model.SignPointPageParam;
import xyz.bali16.module.sign.model.SignPointUpdateParam;
import xyz.bali16.module.sign.service.SignPointService;

import javax.validation.Valid;
import java.util.List;

import static xyz.bali16.application.core.model.Result.*;
import static xyz.bali16.module.sign.utils.ResultUtils.errorLog;

/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@RestController
@RequestMapping(BaseController.API+"sign/point")
@Api(tags = "签到积分活动接口")
@AllArgsConstructor

public class SignPointController extends BaseController{
    public static final Logger logger = LoggerFactory.getLogger(SignPointController.class);

    @Autowired
    SignPointService signPointService;
    @GetMapping
    @ApiOperation("分页条件查询积分列表")
    //@PreAuthorize("@auth.permission('plugin:signpoint:query')")
    public Result<IPage<SignPoint>> pageQuery(@Valid SignPointPageParam param) {
        IPage<SignPoint> tagIPage = signPointService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, tagIPage);
    }


    //@GetMapping
    //////@PreAuthorize("@auth.permission('user:update')")
    //@ApiOperation(value = "查询签到积分列表")
    //public Result<List<SignPoint>> List() {
    //    LambdaQueryWrapper<SignPoint> wrapper = Wrappers.<SignPoint>lambdaQuery()
    //            .orderByDesc(SignPoint::getPoint);
    //    return success(ApiEnum.GET_SUCCESS, signPointService.list(wrapper));
    //}

    @PostMapping
    @ApiOperation("添加签到积分")
    //@PreAuthorize("@auth.permission('plugin:signpoint:add')")
    public Result<String> add(@RequestBody @Valid SignPointAddParam param) {
        try {
            signPointService.addSignPoint(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "新增签到积分 '{}'点,失败 {}", param.getPoint(), e.getMessage());
            return failure(ApiEnum.ADD_ERROR, "新增签到积分失败", e);
        }
    }

    @PutMapping
    @ApiOperation("修改签到积分")
    //@PreAuthorize("@auth.permission('plugin:signpoint:update')")
    public Result<String> update(@RequestBody @Valid SignPointUpdateParam param) {
        try {
            signPointService.updateSignPoint(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "更新签到积分 '{}' 信息失败 {}", param.getId(), e.getMessage());
            return failure(ApiEnum.UPDATE_ERROR, "更新签到积分失败", e);
        }
    }

    @DeleteMapping("/{signId}")
    @ApiOperation("删除签到积分")
    @ApiImplicitParam(name = "signId", value = "签到积分id", paramType = "path", required = true)
    //@PreAuthorize("@auth.permission('plugin:signpoint:delete')")
    public Result<String> deleteNav(@PathVariable("signId") String signId) {
        try {
            signPointService.delete(signId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "删除签到积分 '{}' 失败. {}", signId, e.getMessage());
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }

}
